package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.BaseContext;
import com.itheima.dao.*;
import com.itheima.pojo.*;
import com.itheima.service.OrdersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;


@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersDao, Orders> implements OrdersService {
    @Autowired
    private ShoppingCartDao shoppingCartDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private AddressBookDao addressBookDao;
    @Autowired
    private OrderDetailDao orderDetailDao;
    @Autowired
    private OrdersDao ordersDao;

    /**
     * 用户下单
     * @param orders
     */
    @Override
    public void submit(Orders orders) {
        //order order_detail
        //1.获取用户id
        long userId = BaseContext.getCurrentId();
        //2.查询购物车数据select * from shopping_cart where user_id = ?
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> shoppingCarts = shoppingCartDao.selectList(queryWrapper);
        //3.查询用户数据select * from user where id = ?
        User user = userDao.selectById(userId);
        //4.根据地址id查询地址数据select * from address_book where id = ?
        AddressBook addressBook = addressBookDao.selectById(orders.getAddressBookId());
        long orderId = IdWorker.getId();//订单号
        BigDecimal amount = new BigDecimal(0);//总价
        //5.保存订单明细数据
        for (ShoppingCart shoppingCart : shoppingCarts) {
            OrderDetail orderDetail = new OrderDetail();
        //拷贝数据
            BeanUtils.copyProperties(shoppingCart,orderDetail);
        //orderId单独设置
            orderDetail.setOrderId(orderId);
            //amount 金额单独设置  数量*单价
            BigDecimal price = shoppingCart.getAmount();//单价
            BigDecimal number = new BigDecimal(shoppingCart.getNumber());//数量
            orderDetail.setAmount(price.multiply(number));//总价
            amount.add(orderDetail.getAmount());
            orderDetailDao.insert(orderDetail);
        }
        //6.保存订单数据  有很多数据需要单独处理
        orders.setId(orderId);//订单id
        orders.setOrderTime(LocalDateTime.now());//订单创建时间
        orders.setCheckoutTime(LocalDateTime.now());//订单结账时间
        orders.setStatus(2);//1待付款，2待派送，3已派送，4已完成，5已取消
        orders.setAmount(amount);//总金额
        orders.setUserId(userId);//用户id
        orders.setNumber(String.valueOf(orderId));//订单号
        orders.setUserName(user.getName());//用户名
        orders.setConsignee(addressBook.getConsignee());//收货人
        orders.setPhone(addressBook.getPhone());//收货人联系电话
        orders.setAddress(
                (addressBook.getProvinceName() == null ? "" : addressBook.getProvinceName())
                        + (addressBook.getCityName() == null ? "" : addressBook.getCityName())
                        + (addressBook.getDistrictName() == null ? "" : addressBook.getDistrictName())
                        + (addressBook.getDetail() == null ? "" : addressBook.getDetail()));//省+市+区域+详细地址
        ordersDao.insert(orders);
        //7.删除当前用户的购物车数据
        // delete from shopping_cart where user_id = ?
        LambdaQueryWrapper<ShoppingCart> qw1 = new LambdaQueryWrapper<>();
        qw1.eq(ShoppingCart::getUserId,userId);
        shoppingCartDao.delete(qw1);
    }

}
